package com.wfs.myapp.service.impl;

import com.wfs.myapp.common.ApiResult;
import com.wfs.myapp.entity.domain.bk.BkDish;
import com.wfs.myapp.entity.dto.req.bk.AddDishReqDto;
import com.wfs.myapp.entity.vo.PageResponse;
import com.wfs.myapp.enums.ResponseCodeEnum;
import com.wfs.myapp.repository.bk.DishRepository;
import com.wfs.myapp.service.BkDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;

@Service
@Slf4j
public class BkDishServiceImpl implements BkDishService {

    @Autowired
    private DishRepository dishRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<?> addOrUpdate(Long userId, AddDishReqDto addDishReqDto) {
        BkDish dish;
        if (Objects.nonNull(addDishReqDto.getId())) {
            // 修改
            Optional<BkDish> byId = dishRepository.findById(addDishReqDto.getId());
            if (!byId.isPresent()) return ApiResult.error(ResponseCodeEnum.DISH_NOT_EXIST);
            dish = byId.get();
            BeanUtils.copyProperties(addDishReqDto, dish);
            dish.setUpdatedTime(LocalDateTime.now());
            dish.setUpdatedBy(String.valueOf(userId));
        } else {
            // 添加
            dish = new BkDish();
            BeanUtils.copyProperties(addDishReqDto, dish);
            dish.setUserId(userId);
            dish.setCreatedBy(String.valueOf(userId));
            dish.setUpdatedBy(String.valueOf(userId));
        }
        dish = dishRepository.save(dish);
        return ApiResult.success(dish);
    }

    @Override
    public ApiResult<?> dishList(Long userId, Integer page, Integer size) {
        long total = dishRepository.countByUserId(userId);
        if (total == 0) return ApiResult.success(new PageResponse(new ArrayList(), total, page, size));
        List<BkDish> dishList = dishRepository.findAllByUserId(userId, (page-1)*size, size);
        return ApiResult.success(new PageResponse(dishList, total, page, size));
    }

    @Override
    public ApiResult<BkDish> dishInfo(Long id) {
        Optional<BkDish> dishOpt = dishRepository.findById(id);
        if (!dishOpt.isPresent()) return ApiResult.error(ResponseCodeEnum.DISH_NOT_EXIST);
        BkDish bkDish = dishOpt.get();
        return ApiResult.success(bkDish);
    }

    @Override
    public ApiResult<?> dishInfoByName(String name, Integer page, Integer size) {
        Long total = dishRepository.findCountByName(name);
        List<BkDish> dishList = dishRepository.findAllByName(name);
        if (CollectionUtils.isEmpty(dishList)) ApiResult.success(new PageResponse(new ArrayList(), total, page, size));
        return ApiResult.success(new PageResponse(dishList, total, page, size));
    }

    @Override
    public ApiResult<?> remove(Long userId, AddDishReqDto addDishReqDto) {
        dishRepository.deleteById(addDishReqDto.getId());
        return ApiResult.success();
    }


}
